home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / lib / python2.4 / site-packages / debconf.py < prev    next >
Text File  |  2008-10-10  |  6KB  |  169 lines

  1. # Copyright:
  2. #   Moshe Zadka (c) 2002
  3. #   Canonical Ltd. (c) 2005 (DebconfCommunicator)
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions
  7. # are met:
  8. # 1. Redistributions of source code must retain the above copyright
  9. #    notice, this list of conditions and the following disclaimer.
  10. # 2. Redistributions in binary form must reproduce the above copyright
  11. #    notice, this list of conditions and the following disclaimer in the
  12. #    documentation and/or other materials provided with the distribution.
  13. # THIS SOFTWARE IS PROVIDED BY AUTHORS AND CONTRIBUTORS ``AS IS'' AND
  14. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
  17. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  19. # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  20. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  23. # SUCH DAMAGE.
  24.  
  25. import sys, os
  26. import errno
  27. import re
  28. import popen2
  29. import fcntl
  30.  
  31. class DebconfError(Exception):
  32.     pass
  33.  
  34. LOW, MEDIUM, HIGH, CRITICAL = 'low', 'medium', 'high', 'critical'
  35.  
  36. class Debconf:
  37.  
  38.     def __init__(self, title=None, read=None, write=None):
  39.         for command in ('capb set reset title input beginblock endblock go get'
  40.                         ' register unregister subst fset fget previous_module'
  41.                         ' visible purge metaget exist version settitle'
  42.                         ' info progress data').split():
  43.             self.setCommand(command)
  44.         self.read = read or sys.stdin
  45.         self.write = write or sys.stdout
  46.         sys.stdout = sys.stderr
  47.         self.setUp(title)
  48.  
  49.     def setUp(self, title):
  50.         self.version = self.version(2)
  51.         if self.version[:2] != '2.':
  52.             raise DebconfError(256, "wrong version: %s" % self.version)
  53.         self.capabilities = self.capb().split()
  54.         if title:
  55.             self.title(title)
  56.  
  57.     def setCommand(self, command):
  58.         setattr(self, command,
  59.                lambda *args, **kw: self.command(command, *args, **kw))
  60.  
  61.     def command(self, command, *params):
  62.         command = command.upper()
  63.         self.write.write("%s %s\n" % (command, ' '.join(map(str, params))))
  64.         self.write.flush()
  65.  
  66.         while True:
  67.             try:
  68.                 resp = self.read.readline().rstrip('\n')
  69.                 break
  70.             except IOError, e:
  71.                 if e.errno == errno.EINTR:
  72.                     continue
  73.                 else:
  74.                     raise
  75.  
  76.         if ' ' in resp:
  77.             status, data = resp.split(' ', 1)
  78.         else:
  79.             status, data = resp, ''
  80.         status = int(status)
  81.         if status == 0:
  82.             return data
  83.         elif status == 1:   # unescaped data
  84.             unescaped = ''
  85.             for chunk in re.split(r'(\\.)', data):
  86.                 if chunk.startswith('\\') and len(chunk) == 2:
  87.                     if chunk[1] == 'n':
  88.                         unescaped += '\n'
  89.                     else:
  90.                         unescaped += chunk[1]
  91.                 else:
  92.                     unescaped += chunk
  93.             return unescaped
  94.         else:
  95.             raise DebconfError(status, data)
  96.  
  97.     def stop(self):
  98.         self.write.write('STOP\n')
  99.         self.write.flush()
  100.  
  101.     def forceInput(self, priority, question):
  102.         try:
  103.             self.input(priority, question)
  104.             return 1
  105.         except DebconfError, e:
  106.             if e.args[0] != 30:
  107.                 raise
  108.         return 0
  109.  
  110.     def getBoolean(self, question):
  111.         result = self.get(question)
  112.         return result == 'true'
  113.  
  114.     def getString(self, question):
  115.         return self.get(question)
  116.  
  117.  
  118. class DebconfCommunicator(Debconf, object):
  119.     def __init__(self, owner, title=None, cloexec=False):
  120.         self.dccomm = popen2.Popen3(['debconf-communicate', '-fnoninteractive',
  121.                                      owner])
  122.         super(DebconfCommunicator, self).__init__(title=title,
  123.                                                   read=self.dccomm.fromchild,
  124.                                                   write=self.dccomm.tochild)
  125.         if cloexec:
  126.             fcntl.fcntl(self.read.fileno(), fcntl.F_SETFD, fcntl.FD_CLOEXEC)
  127.             fcntl.fcntl(self.write.fileno(), fcntl.F_SETFD, fcntl.FD_CLOEXEC)
  128.  
  129.     def shutdown(self):
  130.         if self.dccomm is not None:
  131.             self.dccomm.tochild.close()
  132.             self.dccomm.fromchild.close()
  133.             self.dccomm.wait()
  134.             self.dccomm = None
  135.  
  136.     # Don't rely on this; call .shutdown() explicitly.
  137.     def __del__(self):
  138.         try:
  139.             self.shutdown()
  140.         except AttributeError:
  141.             pass
  142.  
  143.  
  144. if ('DEBCONF_USE_CDEBCONF' in os.environ and
  145.     os.environ['DEBCONF_USE_CDEBCONF'] != ''):
  146.     _frontEndProgram = '/usr/lib/cdebconf/debconf'
  147. else:
  148.     _frontEndProgram = '/usr/share/debconf/frontend'
  149.  
  150. def runFrontEnd():
  151.     if not os.environ.has_key('DEBIAN_HAS_FRONTEND'):
  152.         os.environ['PERL_DL_NONLAZY']='1'
  153.         os.execv(_frontEndProgram, [_frontEndProgram, sys.executable]+sys.argv)
  154.  
  155.  
  156. if __name__ == '__main__':
  157.     runFrontEnd()
  158.     db = Debconf()
  159.     db.forceInput(CRITICAL, 'bsdmainutils/calendar_lib_is_not_empty')
  160.     db.go()
  161.     less = db.getBoolean('less/add_mime_handler')
  162.     aptlc = db.getString('apt-listchanges/email-address')
  163.     db.stop()
  164.     print db.version
  165.     print db.capabilities
  166.     print less
  167.     print aptlc
  168.